<?php

namespace seecms\wechat\lib;

use EasyWeChat\OfficialAccount\Application;
use seecms\See;
use seecms\SeeException;

class WechatOfficialAccount
{
    public static function app(array $config = []): Application
    {
        $wechat = See::provider()->general->wechat;

        if (empty($config['app_id'] ?? '')) {
            $config['app_id'] = $wechat['app_id'] ?? '';
        }

        if (empty($config['secret'] ?? '')) {
            $config['secret'] = $wechat['app_secret'] ?? '';
        }

        if (empty($config['secret']) || empty($config['app_id'])) {
            throw new SeeException('appid or secret empty');
        }

        return \EasyWeChat\Factory::officialAccount($config);
    }
}